
import { _decorator, Component, Node, Camera, director, find, LightComponent } from 'cc';
import eventUtil from './framework/modules/eventUtil';
import dataUtil from './framework/modules/dataUtil';
import { uiManager } from './framework/modules/uiManager';
import { httpUtil } from './framework/modules/httpUtil';
import { audioManager } from './framework/modules/audioManager';
import { BundlesConst, EventConst, FormConst } from './const/Const';
import { resourceUtil } from './framework/modules/resourceUtil';
import { BundlesDefine, eventDefine, formDefine, pathDefine } from '../scripts/CustomDefine';
import { memoryConfig } from './framework/modules/memoryConfig';
import { GameStoreDetail } from '../sheets/vo/GameStoreDetail';
import { Debug } from './utils/Debug';
import { homeForm } from '../scripts/home/homeForm';
import { entityUtil } from './framework/modules/entityUtil';
import Common from './utils/Common';
const { ccclass, property } = _decorator;

@ccclass('gameState')
export class gameState {

    public gamePause: boolean = true;
    public gameStarted: boolean = false;

    public respawnNum: number = 0;
    public gameCoin: number = 0;
    public gameScore: number = 0;
    public multiple: number = 1;
    public canRespawnNum: number = 0;
    public player: any;

    private _cameraNode: Camera = null;
    public get townCamera() {
        let scene = director.getScene();
        return scene.getComponentInChildren(Camera);
        let cameraContainer = scene.getChildByName('CameraContainer');
        if (cameraContainer) {
            let cameraNode = cameraContainer.children[0].children[0] as unknown as Node;
            return cameraNode.getComponent(Camera);
        }
        return null;
    };

    constructor() {

        // this.gameLogic = new GameLogic();
        eventUtil.on(EventConst.GAME_STATE_START, this, this.startGame);
        eventUtil.on(EventConst.GAME_STATE_OVER, this, this.gameOver);
        eventUtil.on(EventConst.GAME_STATE_RESPAWN, this, this.respawn);
        eventUtil.on(EventConst.GAME_STATE_RESUME, this, this.resumeGame);
        eventUtil.on(EventConst.GAME_STATE_PAUSE, this, this.pauseGame);
        eventUtil.on(EventConst.GAME_STATE_NEXT, this, this.nextGame);

        eventUtil.on(EventConst.ON_PLATFORM_HIDE, this, this.stopMusic);
        eventUtil.on(EventConst.ON_PLATFORM_SHOW, this, this.playBgMusic);

        //eventUtil.on(EventConst.GAME_ENERGY_CHANGED, this, this.gameBoss);
    }

    public onEnable() {
        gameState._instance = this;
    }
    private static _instance: gameState;
    static get instance(): gameState {
        if (this._instance) {
            return this._instance;
        }
        this._instance = new gameState();
        return this._instance;
    }



    public playBgMusic() {
        if (this.gameStarted) {
            this.stopMusic();
            audioManager.playMusic("BGM");
        }
        // }
    }
    public stopMusic() {
        audioManager.stopMusic();
    }
    public resetGame() {
        this.gamePause = false;
        this.gameStarted = true;
        this.respawnNum = 0;
        this.gameCoin = 0
    }
    public startGame(index: number) {


        memoryConfig.instance.setKey("isYjyScene", 0);

        this.resetGame();
        audioManager.stopMusic();
        this.playBgMusic();

        uiManager.hideUIForm(FormConst.homeForm);
        httpUtil.startGame(dataUtil.getCurrentLevel() + "");
        eventUtil.dispatchEvent(eventDefine.SHOW_LOADING, true);
        entityUtil.showEntity(`level${Common.padLeft("" + (index + 1), 2, "0")}`, uiManager.uiRoot, () => {
            eventUtil.dispatchEvent(eventDefine.SHOW_LOADING, {
                show: false,
                callback: () => {
                    console.log('过渡动画关闭');
                }
            });
        }, BundlesDefine.level)
    }

    public pauseGame() {
        this.gamePause = true;
        // entityUtil.pause();
        // this.touchNode.getComponent('touch').enabled = false;
    }
    public resumeGame() {
        this.gamePause = false;
        // entityUtil.resume();
        // this.touchNode.getComponent('touch').enabled = true;
    }
    public gameOver(e: any) {
        if (!this.gameStarted)
            return;

        this.gameStarted = false;
        // this.vibrate(true);
        eventUtil.dispatchEvent(EventConst.GAME_STATE_PAUSE, null);
        audioManager.stopMusic();
        // httpUtil.endGame(memoryConfig.instance.level + "", e.isWin);
        if (e.isWin && memoryConfig.instance.level == dataUtil.getCurrentLevel()) {
            dataUtil.addCurrentLevel();
        }
        // if (e.isWin)
        //     audioManager.playAudio("win");
        // else
        //     audioManager.playAudio("fail");
        uiManager.hideUIForm(FormConst.gameForm);
        uiManager.pushUIForm(pathDefine.formDir + FormConst.EndForm, () => { }, BundlesDefine.main);
    }
    public nextGame(index: number) {
        if (this.gameStarted) return;
        this.resetGame();
        httpUtil.endGame((index + 1) + "", true);
        this.playBgMusic();
        eventUtil.dispatchEvent(eventDefine.SHOW_LOADING, true);
        entityUtil.showEntity(`level${Common.padLeft("" + (index + 1), 2, "0")}`, uiManager.uiRoot, () => {
            eventUtil.dispatchEvent(eventDefine.SHOW_LOADING, {
                show: false,
                callback: () => {
                    console.log('过渡动画关闭');
                }
            });
        }, BundlesDefine.level)
    }
    public respawn(e: any) {
        if (!this.gameStarted)
            return;
        if (this.gamePause)
            return;
        this.respawnNum++;
        // if (this.respawnNum > this.canRespawnNum) {
        //     eventUtil.sendEventImmediately(EventType.GAME_STATE_OVER, { isWin: false });
        // } else {
        //     this.gamePause = true;
        //     Laya.timer.once(1000, this, () => {
        //         this.gamePause = false;
        //     });
        // }

        this.gamePause = true;
        uiManager.pushUIForm(FormConst.RespawnForm, e)
    }
    public vibrate(isLong: boolean = false) {
        // if (!dataUtil.getVibrateSetting()) return;
        // let rootNode = this.rootNode as Laya.Sprite;
        // let v = isLong ? 10 : 5;
        // let t = 50;
        // let h = Laya.stage.height + v;
        // let w = Laya.stage.width + v;
        // rootNode.pos(0, 0);
        // timeline.reset();
        // timeline.to(rootNode, { x: -v, y: -v }, t)
        //     .to(rootNode, { x: -v, y: v }, t)
        //     .to(rootNode, { x: v, y: -v }, t)
        //     .to(rootNode, { x: v, y: v }, t)
        //     .to(rootNode, { x: 0, y: 0 }, t);
        // timeline.play();
        // if (isLong)
        //     platformUtil.currentPlatform.vibrateLong();
        // else
        //     platformUtil.currentPlatform.vibrateShort();
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
